/*
 * RecordManager.java
 *
 * Created on June 19, 2006, 10:23 AM
 *
 */

package flashomatic;
//
//(c) 2004 Gersham Meharg.
//
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.Font;
import java.util.*;

//This class supports fetching and saving records

class RecordManager {
  
  private static RecordManager instance = null;
  
  public RecordManager() {
  }
  
  public static RecordManager getInstance() {
    if (instance == null)
      instance = new RecordManager();
    return instance;
  };
  
  // Prefs
  // 0 - Simplified Chinese (1),  Traditional Chinese (0)
  // 1 - Lesson
  // 2 - Word Stack
  
  public void SavePrefs(int[] prefs ) throws Exception {
    
    RecordStore rs = null;
    try {
      
      try { RecordStore.deleteRecordStore("prf"); } catch (Exception e){}
      rs = RecordStore.openRecordStore("prf", true);
      if (rs == null) throw new Exception("Failed to open store.");
      
      for (int i = 0; i < prefs.length; i++) {
        String value = new String(String.valueOf(prefs[i]));
        byte[] rsba = value.getBytes();
        rs.addRecord(rsba,0,rsba.length);
      }
      
    } catch (Exception e) {
      throw new Exception(e.getMessage());
      
    } finally {
      try { if (rs != null) rs.closeRecordStore(); } catch (Exception e) {}
      rs = null;
    }
  }
  
  public int[] RestorePrefs() throws Exception {
    
    int[] prefs = null;
    RecordStore rs = null;
    
    try {
      rs = RecordStore.openRecordStore("prf", true);
      if (rs == null) throw new Exception("Failed to open store.");
      int numRecords = rs.getNumRecords();
      if (numRecords == 0) throw new Exception("No records in mcp store.");
      prefs = new int[numRecords];
      
      for (int i = 0; i < numRecords; i++) {
        byte[] rsba = null;
        try { rsba = rs.getRecord(i+1); } catch (Exception e) {}
        if (rsba == null) {
          prefs[i] = 0;
        } else {
          String value = new String(rsba);
          try { prefs[i] = Integer.parseInt(value); } catch (Exception e) {}
          //Output.notice(">> Read Prefs at "+i+" is "+prefs[i]);
        }
      }
      
    } catch (Exception e) {
      
    } finally {
      try { rs.closeRecordStore(); } catch (Exception e) {}
      rs = null;
    }
    
    return prefs;
  }
  
  public void LoadKnowledge(Vector k) {
    
    System.out.println("Loading knowledge cur: "+k.size());
    
    RecordStore rs = null;
    try { rs = RecordStore.openRecordStore("wrd", true); } catch (Exception e) {};
    if (rs == null) {
      System.out.println("Stored knowledge not found");
      return;
    }
    
    int numRecords = 0;
    try {numRecords = rs.getNumRecords(); } catch (Exception e) {}
    if (numRecords == 0) {
      try { if (rs != null) rs.closeRecordStore(); } catch (Exception e) {}
      System.out.println("Empty record store");
      return;
    }
    byte[] rsba = null;
    String value = "";
    
    try {
      System.out.println("Found "+rs.getNumRecords()+" records");
      for (int i = 1; i <= rs.getNumRecords();i++) {
        
        rsba = rs.getRecord(i);
        if (rsba != null) value = new String(rsba);
        
        System.out.println("Loading word: "+value+" at "+i);
        
        k.addElement(value);
        
      }
    } catch (Exception e) {
      System.out.println("Exception reading word "+e.toString());
    }
    
    try { if (rs != null) rs.closeRecordStore(); } catch (Exception e) {}
    
    System.out.println("Loaded "+k.size()+" words");
    
  }
  
  public void SaveKnowlege(Vector v) {
    
    System.out.println("Saving knowledge");
    
    try { RecordStore.deleteRecordStore("wrd"); } catch (Exception e){}
    RecordStore rs = null;
    try { rs = RecordStore.openRecordStore("wrd", true); } catch (Exception e) {}
    if (rs == null) return;
    try {
      for (int i = 0;i < v.size();i++){
        System.out.println("Saving word: "+v.elementAt(i));
        byte[] rsba = String.valueOf(v.elementAt(i)).getBytes();
        rs.addRecord(rsba, 0, rsba.length);
      }
    } catch (Exception e) {}
    
    try { if (rs != null) rs.closeRecordStore(); } catch (Exception e) {}
  }
  
  public String LoadLesson() {
    
    
    String s = null;
    
    RecordStore rs = null;
    try { rs = RecordStore.openRecordStore("lsn", true); } catch (Exception e) {};
    if (rs == null) {
      System.out.println("Stored lesson not found");
      return "/stacks/beg_chinese_lesson1.txt";
    }
    
    int numRecords = 0;
    try {numRecords = rs.getNumRecords(); } catch (Exception e) {}
    if (numRecords == 0) {
      try { if (rs != null) rs.closeRecordStore(); } catch (Exception e) {}
      System.out.println("Empty record store");
      return "/stacks/beg_chinese_lesson1.txt";
    }
    byte[] rsba = null;
    
    try {
      
      rsba = rs.getRecord(1);
      if (rsba != null) s = new String(rsba);
      
      System.out.println("Loading Lesson "+s);
      
    } catch (Exception e) {
      System.out.println("Exception reading word "+e.toString());
    }
    
    try { if (rs != null) rs.closeRecordStore(); } catch (Exception e) {}
    
    return s;
  }
  
  public void SaveLesson(String s) {
    
    System.out.println("Saving Lesson "+s);
    
    try { RecordStore.deleteRecordStore("lsn"); } catch (Exception e){}
    RecordStore rs = null;
    try { rs = RecordStore.openRecordStore("lsn", true); } catch (Exception e) {}
    if (rs == null) return;
    try {
      
      
      byte[] rsba = String.valueOf(s).getBytes();
      rs.addRecord(rsba, 0, rsba.length);
      
    } catch (Exception e) {}
    
    try { if (rs != null) rs.closeRecordStore(); } catch (Exception e) {}
  }
}
